package com.jlf.tool.validation.constraint;

import com.jlf.tool.validation.InString;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;


/**
 * 字符串的有效性校验器
 *
 * @author wujr
 * 2021/8/27
 * @version 1.0
 * 变更历史
 * [wujr 2021/8/27 1.0 新增]
 */
public class InStringConstraintValidator implements ConstraintValidator<InString, String> {
    private InString rule;

    @Override
    public boolean isValid(String string, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isBlank(string)) {
            return true;
        }
        for (String value : rule.value()){
            if (!rule.ignoreCase()) {
                if (string.equals(value)) {
                    return true;
                }
            }else{
                if (string.equalsIgnoreCase(value)){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void initialize(InString annotation){
        this.rule = annotation;
    }
}
